package cn.edu.jssvc.litai.shapedrawabletestmodify;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import cn.edu.jssvc.litai.shapedrawabletestmodify.GradientFragment;
import cn.edu.jssvc.litai.shapedrawabletestmodify.LineFragment;
import cn.edu.jssvc.litai.shapedrawabletestmodify.MainFragment;
import cn.edu.jssvc.litai.shapedrawabletestmodify.OvalFragment;
import cn.edu.jssvc.litai.shapedrawabletestmodify.R;
import cn.edu.jssvc.litai.shapedrawabletestmodify.RectFragment;
import cn.edu.jssvc.litai.shapedrawabletestmodify.RingFragment;

import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    FragmentManager fragmentManager;
    Fragment shapeFragment;
    TextView titleTextView;
    Button nextButton,lastButton;
    int count=1;
    LinearLayout linearLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        linearLayout=findViewById(R.id.layout);
        linearLayout.setVisibility(View.GONE);

        fragmentManager = getSupportFragmentManager();
        Fragment fragment= new MainFragment();
        FragmentTransaction  fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.placehold_frame_layout,fragment);
        fragmentTransaction.commit();



        lastButton=findViewById(R.id.lastBtn);
        lastButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                count--;
                if (count<1){
                    count=1;
                }
                if (count==1){
                    titleTextView.setText("矩形形状可绘制对象示例");
                    shapeFragment = new RectFragment();
                    fragmentManager = getSupportFragmentManager();
                    fragmentManager.beginTransaction().replace(R.id.placehold_frame_layout,shapeFragment).commit();
                    Log.d("count", String.valueOf(count));
                }
                if (count==2){
                    titleTextView.setText("线段形状可绘制对象示例");
                    shapeFragment = new LineFragment();
                    fragmentManager = getSupportFragmentManager();
                    fragmentManager.beginTransaction().replace(R.id.placehold_frame_layout,shapeFragment).commit();
                    Log.d("count", String.valueOf(count));
                }
                if (count==3){
                    titleTextView.setText("椭圆与圆形状可绘制对象示例");
                    shapeFragment = new OvalFragment();
                    fragmentManager = getSupportFragmentManager();
                    fragmentManager.beginTransaction().replace(R.id.placehold_frame_layout,shapeFragment).commit();
                    Log.d("count", String.valueOf(count));
                }
                if (count==4){
                    titleTextView.setText("圆环形状可绘制对象示例");
                    shapeFragment = new RingFragment();
                    fragmentManager = getSupportFragmentManager();
                    fragmentManager.beginTransaction().replace(R.id.placehold_frame_layout,shapeFragment).commit();
                    Log.d("count", String.valueOf(count));
                }
                if (count==5){
                    titleTextView.setText("渐变效果示例");
                    shapeFragment = new GradientFragment();
                    fragmentManager = getSupportFragmentManager();
                    fragmentManager.beginTransaction().replace(R.id.placehold_frame_layout,shapeFragment).commit();
                    Log.d("count", String.valueOf(count));
                }
            }
        });


        nextButton=findViewById(R.id.nextBtn);
        nextButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                count++;
                if (count==1){
                    titleTextView.setText("矩形形状可绘制对象示例");
                    shapeFragment = new RectFragment();
                    fragmentManager = getSupportFragmentManager();
                    fragmentManager.beginTransaction().replace(R.id.placehold_frame_layout,shapeFragment).commit();
                    Log.d("count", String.valueOf(count));
                }
                if (count==2){
                    titleTextView.setText("线段形状可绘制对象示例");
                    shapeFragment = new LineFragment();
                    fragmentManager = getSupportFragmentManager();
                    fragmentManager.beginTransaction().replace(R.id.placehold_frame_layout,shapeFragment).commit();
                    Log.d("count", String.valueOf(count));
                }
                if (count==3){
                    titleTextView.setText("椭圆与圆形状可绘制对象示例");
                    shapeFragment = new OvalFragment();
                    fragmentManager = getSupportFragmentManager();
                    fragmentManager.beginTransaction().replace(R.id.placehold_frame_layout,shapeFragment).commit();
                    Log.d("count", String.valueOf(count));
                }
                if (count==4){
                    titleTextView.setText("圆环形状可绘制对象示例");
                    shapeFragment = new RingFragment();
                    fragmentManager = getSupportFragmentManager();
                    fragmentManager.beginTransaction().replace(R.id.placehold_frame_layout,shapeFragment).commit();
                    Log.d("count", String.valueOf(count));
                }
                if (count==5){
                    titleTextView.setText("渐变效果示例");
                    shapeFragment = new GradientFragment();
                    fragmentManager = getSupportFragmentManager();
                    fragmentManager.beginTransaction().replace(R.id.placehold_frame_layout,shapeFragment).commit();
                    Log.d("count", String.valueOf(count));
                }
                if (count>5){
                    count=5;
                }
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater menuInflater=getMenuInflater();
        menuInflater.inflate(R.menu.select_shape_menu,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {

        linearLayout.setVisibility(View.VISIBLE);

        titleTextView = findViewById(R.id.titleTextView);
        switch (item.getItemId()){
            case R.id.item_rect:
                Log.d("调试", "item_rect1");
                titleTextView.setText("矩形形状可绘制对象示例");
                shapeFragment = new RectFragment();
                fragmentManager = getSupportFragmentManager();
                fragmentManager.beginTransaction().replace(R.id.placehold_frame_layout,shapeFragment).commit();
                count=1;
                Log.d("count", String.valueOf(count));
                break;

            case R.id.item_line:
                Log.d("调试", "item_line1");
                titleTextView.setText("线段形状可绘制对象示例");
                shapeFragment = new LineFragment();
                fragmentManager = getSupportFragmentManager();
                fragmentManager.beginTransaction().replace(R.id.placehold_frame_layout,shapeFragment).commit();
                count=2;
                Log.d("count", String.valueOf(count));
                break;

            case R.id.item_oval:
                Log.d("调试", "item_oval1");
                titleTextView.setText("椭圆与圆形状可绘制对象示例");
                shapeFragment = new OvalFragment();
                fragmentManager = getSupportFragmentManager();
                fragmentManager.beginTransaction().replace(R.id.placehold_frame_layout,shapeFragment).commit();
                count=3;
                Log.d("count", String.valueOf(count));
                break;

            case R.id.item_ring:
                Log.d("调试", "item_ring1");
                titleTextView.setText("圆环形状可绘制对象示例");
                shapeFragment = new RingFragment();
                fragmentManager = getSupportFragmentManager();
                fragmentManager.beginTransaction().replace(R.id.placehold_frame_layout,shapeFragment).commit();
                count=4;
                Log.d("count", String.valueOf(count));
                break;

            case R.id.item_gradient:
                Log.d("调试", "item_gradient1");
                titleTextView.setText("渐变效果示例");
                shapeFragment = new GradientFragment();
                fragmentManager = getSupportFragmentManager();
                fragmentManager.beginTransaction().replace(R.id.placehold_frame_layout,shapeFragment).commit();
                count=5;
                Log.d("count", String.valueOf(count));
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}